//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: filter.c 757 2007-05-10 08:19:31Z roger $
//

/* chinese character filter for old cin.
 * filter uni-char to .bs and char-list to .cmn
 * WARNING : ENCODING : UTF-8*/

#include <string.h>
#include <stdio.h>
#include <ctype.h>

int
main()
{
	FILE *fcin;
	FILE *fbs;
	FILE *fcmn;
	FILE *frevert;
	char buf1[56];
	char buf2[56];
	int len = strlen("阿");

	fprintf(stderr, "length per chinese char : %d\n", len);
	fcin = fopen("wubizixing.cin", "r");

	if (!fcin)
		return 0;

	fbs = fopen("wubizixing.bs", "w");
	if (!fbs) {
		fclose(fcin);
		return 0;
	}

	fcmn = fopen("wubizixing.cmn", "w");

	if (!fbs) {
		fclose(fbs);
		fclose(fcin);
		return 0;
	}

	frevert = fopen("wubizixing.rev", "w");

	/* cin first line */
	fscanf(fcin, "%s", buf1);
	fprintf(fbs, "%s\n", buf1);
	fprintf(fcmn, "%s\n", buf1);

	/* cin ename */
	memset(buf1, 0, sizeof(buf1));
	memset(buf2, 0, sizeof(buf2));
	fscanf(fcin, "%s %s", buf1, buf2);
	fprintf(fbs, "%s %s\n", buf1, buf2);
	fprintf(fcmn, "%s %s\n", buf1, buf2);

	/* cin cname */
	memset(buf1, 0, sizeof(buf1));
	memset(buf2, 0, sizeof(buf2));
	fscanf(fcin, "%s %s", buf1, buf2);
	fprintf(fbs, "%s %s\n", buf1, buf2);

	/* selkey */
	memset(buf1, 0, sizeof(buf1));
	memset(buf2, 0, sizeof(buf2));
	fscanf(fcin, "%s %s", buf1, buf2);
	fprintf(fbs, "%s %s\n", buf1, buf2);

	/* keyname */

	while(1) {
		memset(buf1, 0, sizeof(buf1));
		memset(buf2, 0, sizeof(buf2));
		fscanf(fcin, "%s %s", buf1, buf2);
		fprintf(fbs, "%s %s\n", buf1, buf2);
		if ((strcasecmp(buf1, "%keyname") == 0) &&
				(strcasecmp(buf2, "end") == 0))
			break;
	}

	/* chardef */
	while(1) {
		memset(buf1, 0, sizeof(buf1));
		memset(buf2, 0, sizeof(buf2));
		fscanf(fcin, "%s %s", buf1, buf2);
		if (isalpha(buf2[0])) {
			fprintf(fbs, "%s %s\n", buf1, buf2);
			fprintf(fcmn, "%s %s\n", buf1, buf2);
		} else {
			if (strlen(buf2) > len) {
				fprintf(fcmn, "%s %s\n", buf1, buf2);
			}
			else {
				fprintf(frevert, "%s %s\n", buf2, buf1);
				fprintf(fbs, "%s %s\n", buf1, buf2);
			}
		}
		if ((strcasecmp(buf1, "%chardef") == 0) &&
				(strcasecmp(buf2, "end") == 0))
			break;
	}

	fclose(fcin);
	fclose(fbs);
	fclose(fcmn);
	fclose(frevert);

	return 1;
}

// vi:ts=4:nowrap:cin:expandtab
